<?php
namespace Home\Controller;

//---------------
//Author:Hua
//--------------

class UserController extends CommonController {

    /*前台用户个人中心*/
    public function index(){
        $this->display();
    }

    /*个人资料修改*/
    public function info(){
        if(IS_POST){
            $list = D('User');
            $list->create();
            $list->save();

            $this->success('修改成功','info');
        }else{
            $list = D('User');
            $info = $list->where("id={$_SESSION[login][id]}")->getInfo();
            //dump($info);exit;

            $this->assign('info',$info);
            $this->display();
        }
    }

    /*头像上传*/
    public function photo(){

        $upload = new \Think\Upload();// 实例化上传类    
        $upload->maxSize   =     3145728 ;// 设置附件上传大小    
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
        $upload->savePath  =      './Public/'; // 设置附件上传目录
        $upload->autoSub   =     false;    

        $info   =   $upload->upload();    
        if(!$info) {// 上传错误提示错误信息        
            $this->error($upload->getError());    
        }else{// 上传成功        
            foreach($info as $file){       
                $photo = $file['savepath'].$file['savename'];
            }   
        }

        $user = D('User');
        $info = $user->where("id={$_SESSION[login][id]}")->find();
        $user->create(); // 创建数据对象
        $user->photo = $file["savename"]; //保存上传的照片根据需要自行组装
        $user->id = $info['id'];
        $user->save(); // 写入用户数据到数据库

        $user = D('User');
        $info2 = $user->where("id={$_SESSION[login][id]}")->find();
        $this->assign('info',$info2);
        $this->display('User/info');
       
        $this->redirect('User/info');
    }

    /*密码修改*/
    public function pass(){
        $data['id'] = $_SESSION['login']['id'];
        $oldpass['oldpass'] = md5($_POST['oldpass']);
        $data['pass'] = md5($_POST['pass']);
        $repass['repass'] = $_POST['repass'];

        if($oldpass['oldpass'] != "{$_SESSION['login']['pass']}"){
            $this->error('原密码输入错误');
        }
        if($_POST['pass'] != $_POST['repass']){
            $this->error('前后两次密码输入不一致');
        }
        $list = D('User');
        if($list->save($data)){
            $this->success('密码修改成功','info');
        }
    }

    /*添加收藏*/
    public function attention(){
        $data['goodsid'] = $_GET['id'];
        $data['userid'] = $_SESSION['login']['id'];

        $coll = D('collects');
        $info = $coll->where($data)->find();
        //dump($info);exit;
        if(empty($info)){
            $data['addtime'] = time();
            $coll->add($data);
            $this->redirect('atten');
        }{
            $this->error('该商品已收藏');
        }
 
    }

    /*遍历收藏商品*/
    public function atten(){
        $collect = D('Collects');
        $goods = D('Goods');
        $info = $collect->where("userid={$_SESSION['login']['id']}")->select();
        //dump($info);
        foreach($info as $key=>$val){
            $info[$key]['goods'] = $goods->where("id=".$val['goodsid'])->find()['goods'];
            $info[$key]['num'] = $goods->where("id=".$val['goodsid'])->find()['num'];
            $info[$key]['picname'] = $goods->where("id=".$val['goodsid'])->find()['picname'];
            $info[$key]['price'] = $goods->where("id=".$val['goodsid'])->find()['price'];
            $info[$key]['state'] = $goods->where("id=".$val['goodsid'])->find()['state'];
        }
        //dump($info);
        $state = array('新添加','在售','下架');
        foreach($info as $key=>$val){
            date_default_timezone_set("PRC");
            $info[$key]['addtime'] = date('Y-m-d H:i:s',$info[$key]['addtime']);
            $info[$key]['state'] = $state[$val['state']];
        }
        //dump($info);
        if(empty($info))$info[]['sg']=1;
        $this->assign('info',$info);
        $this->display();
    }

    /*删除收藏商品*/
    public function del(){
        $id = $_GET['id'];
        $collect = D('Collects');
        if($collect->delete($id)){
            $this->redirect('atten');
        }
    }

}